package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.joint.base.bean.EnumManage;
import com.joint.base.bean.SystemConfig;
import com.joint.base.entity.Users;
import com.joint.base.entity.system.Admin;
import com.joint.base.mp.WxMpInCacheConfigStorage;
import com.joint.base.service.AdminService;
import com.joint.base.service.UsersService;
import com.joint.base.service.jms.AdvancedNotifyMessageProducer;
import com.joint.base.util.DataUtil;
import com.joint.core.entity.ProAttend;
import com.joint.core.entity.ProInfo;
import com.joint.core.service.ProAttendService;
import com.joint.core.service.ProInfoService;
import com.joint.web.action.BaseAdminAction;
import fz.me.chanjar.weixin.common.api.WxConsts;
import fz.me.chanjar.weixin.mp.bean.WxMpCustomMessage;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 后台Action类 - 微信首页
 * ============================================================================
 * 版权所有 2014 min_xu。
 * ----------------------------------------------------------------------------
 * 
 * @author zhucx
 * 
 * @version 1.1 2015-6-8
 */

@ParentPackage("mp")
public class JobAction extends BaseAdminAction {
	private static final long serialVersionUID = -5383463207248344971L;
    @Resource
    private ProAttendService proAttendService;
    @Resource
    private ProInfoService proInfoService;
    @Resource
    private WxMpInCacheConfigStorage wxMpConfigStorage;
    @Resource
    private SystemConfig systemConfig;
    @Resource
    private AdvancedNotifyMessageProducer notifyMessageProducer;
    @Resource
    private UsersService usersService;
    @Resource
    private AdminService adminService;

    public void doProAttendDeal(){
        Date now = new Date();
        String stime = DataUtil.DateToString(now, "HH:mm:ss");
        now = DataUtil.StringToDate(stime, "HH:mm:ss");
        Pager pager = getPager();
        Users users = usersService.getLoginInfo();
        String username = users.getName();
        Admin admin = adminService.getByMobile(username);
        String openId = admin.getOpenId();
        Map<String,Object> params= Maps.newHashMap();
        params.put("proState",0);
        params.put("state", BaseEnum.StateEnum.Enable);
        pager=proInfoService.findByPagerAndLimit(pager,users,usersService.getCompanyByUser(),params);
        List<ProInfo> listProInfo = (List<ProInfo>) (pager.getTotalCount()>0?pager.getList(): Lists.newArrayList());
        for(int i=0; i<listProInfo.size();i++){
            ProInfo proInfo = listProInfo.get(i);
            List<ProAttend> listProAttend = proAttendService.getListByProInfo(proInfo,new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
            for(int j=0; j<listProAttend.size();j++){
                ProAttend proAttend = listProAttend.get(j);
                Date regularStart  = proAttend.getRegularStart();
                String regularStartime = DataUtil.DateToString(regularStart, "HH:mm");
                long rs = DataUtil.TimeDiff(regularStart, now);
                if(rs < 1000*60*61 && rs >=1000*60*60){
                    sendMessage(openId,proInfo.getName(),regularStartime);

                }

            }
        }

    }

    /**
     * 获得分页器
     * @return
     */
    public Pager getPager(){
        Pager pager = new Pager();
        pager.setPageNumber(0);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.desc);
        return pager;
    }

    /**
     * 发送消息
     * @param openId
     */
    public void sendMessage(String openId, String proInfoName,String regularStartTime){
        //发送一个mp的客服消息，利用JMS，这里暂时测试用
        WxMpCustomMessage message = new WxMpCustomMessage();
        message.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
        message.setToUser(openId);
        //String link = "<a href=\""+wxMpConfigStorage.getHttpHost()+"/"+systemConfig.getWebroot()+"/mp/account!resetMp.action?_="+ Identities.randomBase62(8)+"\">设置我的账号</a>";
        message.setContent("[微笑] 您的工程项目'" + proInfoName+ "' 签到开始时间为"+regularStartTime+ ",请及时签到" );
        notifyMessageProducer.sendQueue(message, EnumManage.NotifyKeyEnum.wxMpCustomMessage.name());
    }


}
